package com.skivingcloud.report.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.skivingcloud.admin.utils.UserUtil;
import com.skivingcloud.common.utils.UuidUtil;
import com.skivingcloud.report.constants.ReportConstant;
import com.skivingcloud.report.entity.ReportDataSet;
import com.skivingcloud.report.entity.ReportDataSetParam;
import com.skivingcloud.report.entity.ReportDataSetTransform;
import com.skivingcloud.report.mapper.ReportDataSetMapper;
import com.skivingcloud.report.mapper.ReportDataSetParamMapper;
import com.skivingcloud.report.mapper.ReportDataSetTransformMapper;
import com.skivingcloud.report.service.ReportDataSetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.skivingcloud.common.utils.PageParameter;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 数据集管理 服务层实现。
 *
 * @author skivingcloud
 * @since 2024-09-12
 */
@Service
public class ReportDataSetServiceImpl extends ServiceImpl<ReportDataSetMapper, ReportDataSet> implements ReportDataSetService {

    private ReportDataSetParamMapper reportDataSetParamMapper;
    private ReportDataSetTransformMapper reportDataSetTransformMapper;

    public ReportDataSetServiceImpl(ReportDataSetParamMapper reportDataSetParamMapper, ReportDataSetTransformMapper reportDataSetTransformMapper) {
        this.reportDataSetParamMapper = reportDataSetParamMapper;
        this.reportDataSetTransformMapper = reportDataSetTransformMapper;
    }

    /**
     * 分页查询数据集管理
     *
     * @param parameter 查询参数
     * @return 分页查询结果
     */
    @Override
    public Page<ReportDataSet> queryByPage(PageParameter<ReportDataSet> parameter){
        Page<ReportDataSet> page = new Page<>(parameter.getPage(), parameter.getPageSize());
        QueryWrapper queryWrapper = QueryWrapper.create();
        if (parameter.getParam() != null) {
            //查询条件
            if (StringUtils.isNoneBlank(parameter.getParam().getId())) {
                queryWrapper.and(ReportDataSet::getId).like(parameter.getParam().getId());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getSetCode())) {
                queryWrapper.and(ReportDataSet::getSetCode).like(parameter.getParam().getSetCode());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getSetName())) {
                queryWrapper.and(ReportDataSet::getSetName).like(parameter.getParam().getSetName());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getSetDesc())) {
                queryWrapper.and(ReportDataSet::getSetDesc).like(parameter.getParam().getSetDesc());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getSourceCode())) {
                queryWrapper.and(ReportDataSet::getSourceCode).like(parameter.getParam().getSourceCode());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getDynSentence())) {
                queryWrapper.and(ReportDataSet::getDynSentence).like(parameter.getParam().getDynSentence());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getCaseResult())) {
                queryWrapper.and(ReportDataSet::getCaseResult).like(parameter.getParam().getCaseResult());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getEnableFlag())) {
                queryWrapper.and(ReportDataSet::getEnableFlag).like(parameter.getParam().getEnableFlag());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getDeleteFlag())) {
                queryWrapper.and(ReportDataSet::getDeleteFlag).like(parameter.getParam().getDeleteFlag());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getCreateBy())) {
                queryWrapper.and(ReportDataSet::getCreateBy).like(parameter.getParam().getCreateBy());
            }
            if (StringUtils.isNoneBlank(parameter.getParam().getSetType())) {
                queryWrapper.and(ReportDataSet::getSetType).like(parameter.getParam().getSetType());
            }
        }
        return this.page(page, queryWrapper);
    }

    /**
     * 新增包含参数和转换
     *
     * @param reportDataSet 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveDataSet(ReportDataSet reportDataSet) {
        LocalDateTime nowTime = LocalDateTime.now();
        reportDataSet.setId(UuidUtil.get32UUID());
        reportDataSet.setCreateBy(UserUtil.getCurrentUser().getId());
        reportDataSet.setCreateTime(nowTime);
        reportDataSet.setEnableFlag(ReportConstant.REPORT_ENABLE_FLAG_E);
        reportDataSet.setDeleteFlag(ReportConstant.COMMON_YES_OR_NO_NO);
        this.mapper.insert(reportDataSet, true);
        reportDataSet.getDataSetParamList().forEach(param -> {
            param.setSetCode(reportDataSet.getId());
            param.setCreateBy(UserUtil.getCurrentUser().getId());
            param.setCreateTime(nowTime);
            param.setEnableFlag(ReportConstant.REPORT_ENABLE_FLAG_E);
            param.setDeleteFlag(ReportConstant.COMMON_YES_OR_NO_NO);
            reportDataSetParamMapper.insert(param, true);
        });
        reportDataSet.getDataSetTransformList().forEach(trans -> {
            trans.setSetCode(reportDataSet.getId());
            trans.setCreateBy(UserUtil.getCurrentUser().getId());
            trans.setCreateTime(nowTime);
            trans.setEnableFlag(ReportConstant.REPORT_ENABLE_FLAG_E);
            trans.setDeleteFlag(ReportConstant.COMMON_YES_OR_NO_NO);
            reportDataSetTransformMapper.insert(trans, true);
        });
        return true;
    }

    /**
     * 修改包含参数和转换
     *
     * @param reportDataSet 参数
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateDataSetById(ReportDataSet reportDataSet) {
        LocalDateTime nowTime = LocalDateTime.now();
        reportDataSet.setUpdateBy(UserUtil.getCurrentUser().getId());
        reportDataSet.setUpdateTime(nowTime);
        this.mapper.update(reportDataSet, true);
        reportDataSetParamMapper.deleteByQuery(
                new QueryWrapper().where(ReportDataSetParam::getSetCode).eq(reportDataSet.getId()));
        reportDataSetTransformMapper.deleteByQuery(
                new QueryWrapper().where(ReportDataSetTransform::getSetCode).eq(reportDataSet.getId()));
        reportDataSet.getDataSetParamList().forEach(param -> {
            param.setSetCode(reportDataSet.getId());
            param.setCreateBy(UserUtil.getCurrentUser().getId());
            param.setCreateTime(nowTime);
            param.setEnableFlag(ReportConstant.REPORT_ENABLE_FLAG_E);
            param.setDeleteFlag(ReportConstant.COMMON_YES_OR_NO_NO);
            reportDataSetParamMapper.insert(param, true);
        });
        reportDataSet.getDataSetTransformList().forEach(trans -> {
            trans.setSetCode(reportDataSet.getId());
            trans.setCreateBy(UserUtil.getCurrentUser().getId());
            trans.setCreateTime(nowTime);
            trans.setEnableFlag(ReportConstant.REPORT_ENABLE_FLAG_E);
            trans.setDeleteFlag(ReportConstant.COMMON_YES_OR_NO_NO);
            reportDataSetTransformMapper.insert(trans, true);
        });
        return true;
    }

    /**
     * 删除包含参数和转换
     *
     * @param id id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean removeDataSetById(Serializable id) {
        this.mapper.deleteById(id);
        reportDataSetParamMapper.deleteByQuery(
                new QueryWrapper().where(ReportDataSetParam::getSetCode).eq(id));
        reportDataSetTransformMapper.deleteByQuery(
                new QueryWrapper().where(ReportDataSetTransform::getSetCode).eq(id));
        return true;
    }

    /**
     * 根据数据集管理主键获取详细信息
     *
     * @param id 主键
     * @return 结果
     */
    @Override
    public ReportDataSet getInfo(Serializable id) {
        ReportDataSet reportDataSet = this.mapper.selectOneById(id);
        reportDataSet.setDataSetParamList(reportDataSetParamMapper.selectListByQuery(
                new QueryWrapper().where(ReportDataSetParam::getSetCode).eq(id)
        ));
        reportDataSet.setDataSetTransformList(reportDataSetTransformMapper.selectListByQuery(
                new QueryWrapper().where(ReportDataSetTransform::getSetCode).eq(id)
        ));
        return reportDataSet;
    }
}
